//
//  CommomView.swift
//  CosClub
//
//  Created by 沙睿 on 2020/2/12.
//  Copyright © 2020 ios. All rights reserved.
//

import Foundation
import RxSwift
import RxCocoa
import SnapKit


//MARK: - NavigationBar

public struct NavBarLayout : Layoutable {
    public func layoutMaker(inset: Int = 0) -> (ConstraintMaker) -> Void {
        return { maker in
            maker.leading.top.trailing.equalTo(self.superView)
            maker.height.equalTo(self.height)
        }
    }
   
    public var height :CGFloat
    public var superView : UIView
    public init(height: CGFloat, superView:UIView) {
        self.height = height
        self.superView = superView
    }
}

public struct NavbarRightItemLayout: Layoutable {
    public func layoutMaker(inset: Int = 0) -> (ConstraintMaker) -> Void {
        return { maker in
            maker.trailing.equalToSuperview().inset(inset)
            maker.size.equalTo(self.size)
            maker.bottom.equalToSuperview().inset((44 - self.size.height)/2)
        }
    }
    var size :CGSize
    public init(size: CGSize) {
        self.size = size
    }
}

public protocol Layoutable {
    func layoutMaker(inset: Int) ->(ConstraintMaker) -> Void
}

extension UIView{
    public func makeLayout(_ layouter:Layoutable, inset: Int = 0) {
        snp.makeConstraints(layouter.layoutMaker(inset: inset))
    }
}

public class NavView: UIView {
    
    public let line = UIView(type: .line)
    
    private var backBlock: (()-> ())? = nil
    private var disposeBag = DisposeBag()
    private var headPortrait: UIImageView?
    private var titleLabel: UILabel?
    
    public enum BackType {
        case gary
        case white
        case room
        case black
    }
    
    public var headerUrl: String? {
        didSet {
            self.headPortrait?.url_image(headerUrl,
                                         placeholder: UIImage.imageFromBasis(named: "placeholder_userheader"))
            self.headPortrait?.isHidden = false
        }
    }
    
    public var title: String? {
        didSet {
            self.titleLabel?.text = self.title
            self.titleLabel?.isHidden = !(self.title?.count ?? 0 > 0)
        }
    }
    
    public init(type: BackType = .white , back:(() -> ())?) {
        
        super.init(frame: CGRect.zero)
        self.backgroundColor = UIColor.white
        // 返回
        let backBtn = UIButton.init(type: .custom)
        self.addSubview(backBtn)
        
        backBtn.snp.makeConstraints { (make) in
            make.left.bottom.equalToSuperview()
            make.width.height.equalTo(44)
        }
        
        self.backBlock = back
        backBtn.isHidden = (self.backBlock == nil)
        backBtn.rx.tap.subscribe(onNext: { [weak self](even) in
            guard (self?.backBlock) != nil else {
                return
            }
            self?.backBlock!()
        }).disposed(by: self.disposeBag)
        
        
        var backImage = "nav_back"
        var backgroud = UIColor.app.white
        
        switch type {
        case .white:
            creatNavitionBarDefault()
        case .black:
            backImage = "nav_back_white"
            creatNavitionBarDefault()
            self.titleLabel?.textColor = UIColor.app.white
            backgroud = UIColor.app.bg_D0
            
        case .gary:
            creatNavitionBarDefault()
        case .room:
            backImage = "nav_back_gray"
            backgroud = UIColor.clear
            creatNavitionBarRoom()
        }
        
        backBtn.setImage(UIImage.imageFromBasis(named: backImage), for: .normal)
        self.backgroundColor = backgroud
        
        
    }
    
    func creatNavitionBarRoom() {
        let title = UILabel("", color: UIColor.app.c3c, fontSize: 16, type: .bold)
        self.addSubview(title)
        
        title.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(44)
            make.height.equalTo(44)
            make.width.greaterThanOrEqualTo(0)
            make.bottom.equalTo(self.snp.bottom).inset(0)
        }
        self.titleLabel = title
        
        let descript = UILabel("观众212", color: UIColor.app.c3c, fontSize: 10)
        self.addSubview(descript)
        
        descript.snp.makeConstraints { (make) in
            make.left.equalTo(title.snp.right).offset(18)
            make.height.equalTo(44)
            make.width.greaterThanOrEqualTo(0)
            make.bottom.equalTo(self.snp.bottom).inset(0)
        }
        
        
    }
    
    func creatNavitionBarDefault() {
        //头像
        let headPortrait = UIImageView.init()
        headPortrait.backgroundColor = UIColor.app.c1
        headPortrait.isHidden = true
        headPortrait.frame = CGRect(x: 0, y: 0, width: 28, height: 28)
        headPortrait.sr_cornerRadius = 14
        self.headPortrait = headPortrait
        self.addSubview(headPortrait)
        
        headPortrait.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.width.height.equalTo(28)
            make.bottom.equalTo(self.snp.bottom).inset(8)
        }
        
        //头像
        let title = UILabel("", color: UIColor.app.c3a, fontSize: 18, type: .bold)
        self.addSubview(title)
        
        title.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.height.equalTo(28)
            make.width.greaterThanOrEqualTo(0)
            make.bottom.equalTo(self.snp.bottom).inset(8)
        }
        self.titleLabel = title
        
        
        addSubview(line)
        
        line.snp.makeConstraints { (maker) in
            maker.height.equalTo(0.5)
            maker.leading.trailing.bottom.equalToSuperview()
        }
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK: - 默认左图右字
public class IconNumber: UIView {
    
    public let textLabel :UILabel
    public let icon: UIButton
    
    public var text: String? {
        didSet {
            textLabel.text = text
            textLabel.sizeToFitWidth()
            self.width = textLabel.frame.maxX
            self.snp.updateConstraints { (maker) in
                maker.width.equalTo(self.width)
            }
            self.setNeedsLayout()
            self.layoutIfNeeded()
        }
    }
    
    public init(height: Int,
                img: UIImage?,
                text: String,
                fontSize: CGFloat = 10,
                spacing: Int = 5) {
        
        textLabel = UILabel("", color: UIColor.app.c3b, fontSize: fontSize)
        icon = UIButton()
        icon.setImage(img, for: .normal)
        
        icon.sizeToFit()
        
        super.init(frame:CGRect(x: 0, y: 0, width: 0, height: height))
        addSubview(icon)
        addSubview(self.textLabel)
        
        self.icon.x = 0
        self.icon.y = (self.height - icon.height)/2
        
        self.textLabel.frame = CGRect(x: Int(self.icon.frame.maxX) + spacing,
                                      y: 0,
                                      width: 0,
                                      height: Int(self.height)
        )
        
        self.textLabel.text = text
        self.textLabel.sizeToFitWidth()
        self.width = textLabel.frame.maxX
        
        self.snp.makeConstraints { (maker) in
            maker.width.equalTo(self.width)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}




// MARK:  默认View
public class DefaultItemsView: UIButton {
    public enum ItemType {
        case nomal
        case tag
    }

    public var title : String?
    public var descText : String?
    public convenience init(title: String?, descText: String?, type: ItemType = .nomal) {
        self.init(type: .custom)
        self.backgroundColor = UIColor.white
        self.title = title
        self.descText = descText
        creatItem(title: title, desc: descText)
    }
    
    public func creatItem(title: String?, desc: String?){
        
        let title = UILabel(title,fontSize:15)
        self.addSubview(title)
        title.snp.makeConstraints { (maker) in
            maker.leading.top.bottom.equalToSuperview().inset(UISpecification.marginHor.rawValue)
            maker.width.greaterThanOrEqualTo(0)
        }
        
        let descText = UILabel(desc,color:UIColor.app.c3b,fontSize:14)
        self.addSubview(descText)
        descText.snp.makeConstraints { (maker) in
            maker.top.bottom.equalToSuperview().inset(16)
            maker.trailing.equalToSuperview().inset(34)
            maker.width.greaterThanOrEqualTo(0)
        }
        
        let icon = UIImageView.init(image: UIImage.imageFromBasis(named: "cm_arrow_right"))
        self.addSubview(icon)
        icon.snp.makeConstraints { (maker) in
            maker.size.equalTo(CGSize(width: 8, height: 12))
            maker.centerY.equalToSuperview()
            maker.trailing.equalToSuperview().inset(15)
        }
        
        let lineView = UIView(type: .line)
        self.addSubview(lineView)
        lineView.snp.makeConstraints { (maker) in
            maker.leading.equalToSuperview().inset(UISpecification.marginHor.rawValue)
            maker.bottom.trailing.equalToSuperview()
            maker.height.equalTo(0.5)
        }
        
    }
}


